package com.ln.asset.core.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ln.asset.common.result.R;
import com.ln.asset.core.pojo.AssetsFixation;
import com.ln.asset.core.service.AssetsFixationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xueshaorui
 * @since 2022-05-03
 */
@Api(tags = "固定资产")
@RestController
@RequestMapping("/admin/assetsFixation")
@CrossOrigin
@Slf4j
public class AssetsFixationController {
    @Resource
    private AssetsFixationService assetsFixationService;
    @ApiOperation("全查")
    @GetMapping("/list")
    public R list(){
        List<AssetsFixation> list = assetsFixationService.list(null);
        return R.ok().data("list",list);
    }

    @ApiOperation("新增固定资产")
    @PostMapping("/save")
    public R save(
             @RequestBody AssetsFixation assetsFixation){
        boolean save = assetsFixationService.save(assetsFixation);
        if (save) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation("删除固定资产")
    @DeleteMapping("/remove/{id}")
    public R removeId(
            @ApiParam(value = "车辆id",example = "100",required = true)
            @PathVariable Integer id){

        boolean result = assetsFixationService.removeById(id);
        if (result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }
    @ApiOperation("根据id获取固定资产")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value = "数据id", required = true, example = "1")
            @PathVariable Long id
    ){
        AssetsFixation byId = assetsFixationService.getById(id);
        if(byId != null){
            return R.ok().data("record", byId);
        }else{
            return R.error().message("数据不存在");
        }
    }
    @ApiOperation("更新固定资产")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(value = "车辆对象",required = true)
            @RequestBody AssetsFixation assetsFixation){

        boolean result = assetsFixationService.updateById(assetsFixation);
        if(result){
            return R.ok().message("更新成功");
        }else {
            return R.error().message("更新失败");
        }
    }

    @ApiOperation("获取分页列表")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit) {

        Page<AssetsFixation> pageParam = new Page<>(page, limit);
        IPage<AssetsFixation> pageModel = assetsFixationService.listPage(pageParam);
        return R.ok().data("pageModel", pageModel);
    }
}

